package com.itls.recommendedgames.service.Impl;

import com.itls.recommendedgames.entity.UserLibrary;
import com.itls.recommendedgames.entity.UserLibraryGameDTO;
import com.itls.recommendedgames.mapper.UserLibraryMapper;
import com.itls.recommendedgames.service.UserLibraryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class UserLibraryServiceImpl implements UserLibraryService {
    @Resource
    private UserLibraryMapper userLibraryMapper;

    @Override
    public boolean addToLibrary(UserLibrary userLibrary) {
        // 检查是否已拥有
        UserLibrary existing = userLibraryMapper.findByUserAndGame(userLibrary.getUserId(), userLibrary.getGameId());
        if (existing != null) {
            return false;
        }
        // 插入新记录
        userLibraryMapper.insert(userLibrary);
        return true;
    }

    public List<Integer> getUserGameIds(UserLibrary userLibrary) {
        return userLibraryMapper.getUserGameIds(userLibrary.getUserId());
    }

    @Override
    public boolean updatePlayTime(Integer userId, Integer gameId, BigDecimal playTimeHours) {
        return userLibraryMapper.updatePlayTime(userId, gameId, playTimeHours);
    }

    @Override
    public List<UserLibraryGameDTO> getUserLibrary(Integer userId) {
        return userLibraryMapper.getUserLibraryWithTags(userId);
    }

    @Override
    public boolean removeGame(Integer userId, Integer gameId) {
        // 检查游戏是否存在于用户库中
        UserLibrary existing = userLibraryMapper.findByUserAndGame(userId, gameId);
        if (existing == null) {
            return false; // 游戏不存在于用户库中
        }
        
        // 删除游戏
        return userLibraryMapper.removeGame(userId, gameId);
    }
}
